package com.runmi.webchat.aftersale.web.shop.service;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.runmi.webchat.aftersale.common.result.Result;
import com.runmi.webchat.aftersale.common.result.ResultFactory;
import com.runmi.webchat.aftersale.dbcore.entity.Role;
import com.runmi.webchat.aftersale.dbcore.entity.ShopInfo;
import com.runmi.webchat.aftersale.dbcore.entity.User;
import com.runmi.webchat.aftersale.dbcore.mapper.AdminMenuMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.RoleMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.RoleMenuMapper;
import com.runmi.webchat.aftersale.dbcore.mapper.UserMapper;
import com.runmi.webchat.aftersale.util.Constants;
import com.runmi.webchat.aftersale.dbcore.mapper.ShopInfoMapper;
import com.runmi.webchat.aftersale.web.base.BaseService;
import com.runmi.webchat.aftersale.web.set.model.UserEdit;
import com.runmi.webchat.aftersale.web.shop.vo.ShopManageEditReqVo;
import com.runmi.webchat.aftersale.web.shop.vo.ShopManageIndexReqVo;

@Service
public class ShopManageService extends BaseService{


	
	@Autowired
	private RoleMapper roleMapper;
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private AdminMenuMapper adminMenuMapper;
	
	@Autowired
	private RoleMenuMapper roleMenuMapper;
	
	@Autowired
	private ShopInfoMapper shopInfoMapper;
	
	public void index(ShopManageIndexReqVo model){
		
		try{		
				
		    PageHelper.startPage(model.getCpage(), model.getPageSize());
			List<Map> items=shopInfoMapper.getShopInfoByCodition(
					model.getShopName(), 
					model.getShopCode(),
					model.getProvinceId(), 
					model.getCityId(),
					model.getAreaId());
			PageInfo<Map> pageInfo = new PageInfo<Map>(items);
			model.setTotalCount((int)pageInfo.getTotal());
			for(Map item:items){
				 if(null==item.get("code_url1")){
					 item.put("code1", "0");
				 }else{
					 String u=item.get("code_url1").toString();
					 if(StringUtil.isNotEmpty(u)){
						 item.put("code1", "1");
					 }else{
						 item.put("code1", "0");
					 }
				 }
				 if(null==item.get("code_url2")){
					 item.put("code2", "0");
				 }else{
					 String u=item.get("code_url2").toString();
					 if(StringUtil.isNotEmpty(u)){
						 item.put("code2", "1");
					 }else{
						 item.put("code2", "0");
					 }
				 }
				 if(null==item.get("code_url3")){
					 item.put("code3", "0");
				 }else{
					 String u=item.get("code_url3").toString();
					 if(StringUtil.isNotEmpty(u)){
						 item.put("code3", "1");
					 }else{
						 item.put("code3", "0");
					 }
				 }
			}
			model.setItems(items);
		}catch(Exception ex){
			ex.printStackTrace();
			
		}
	}
	
	public void edit(ShopManageEditReqVo model){
					
		ShopInfo shopInfo= null;
		if(null!=model.getShopId()){
			shopInfo=shopInfoMapper.selectByPrimaryKey(model.getShopId());
		}
		shopInfo=shopInfo==null?new ShopInfo():shopInfo;
		model.setShopInfo(shopInfo);
		
	}
	
	@Transactional
	public Result saveShop(
			Integer id, 
			String shopCode, String shopName, 
			Integer provinceId, Integer cityId, Integer areaId, String address,
			String contactName,
			String contactMobile,
			User currentUser) {
		try {
			

			Date currentDate = Calendar.getInstance().getTime();
			
			if (null == id) {
				ShopInfo shopInfo = new ShopInfo();
				shopInfo.setProvinceId(provinceId);
				shopInfo.setCityId(cityId);
				shopInfo.setAreaId(areaId);
				shopInfo.setShopName(shopName);
				shopInfo.setAddress(address);
				shopInfo.setShopCode(shopCode);
				shopInfo.setCreateTime(currentDate);
				shopInfo.setStatus((byte) 1);
				shopInfo.setDelFlag((byte)0);
				shopInfo.setContactMobile(contactMobile);
				shopInfo.setContactName(contactName);
				shopInfoMapper.insert(shopInfo);
			} else {
				ShopInfo shopInfo = shopInfoMapper.selectByPrimaryKey(Integer.valueOf(id));

				shopInfo.setProvinceId(provinceId);
				shopInfo.setCityId(cityId);
				shopInfo.setAreaId(areaId);
				shopInfo.setShopName(shopName);
				shopInfo.setAddress(address);
				shopInfo.setShopCode(shopCode);
				shopInfo.setContactMobile(contactMobile);
				shopInfo.setContactName(contactName);
				shopInfoMapper.updateByPrimaryKeySelective(shopInfo);
			}

			
			return ResultFactory.getResultSuccess();

		} catch (Exception e) {
			logErr.error("保存客户档案参数异常", e);	
			return ResultFactory.getResultFail("系统错误");

		}
	}
	
	
	public Map<String,String> changeStatus(User erpUser, Integer shopId,Byte opType){
		Map<String,String> result=new HashMap<String,String>();
		try{
			this.log.info("用户" + erpUser.getRealName() + " 操作:" + shopId + " 操作类型:" + opType );
			if(shopId==null || null==opType){
				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
				result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");					
				return result;
			}

			if(opType.intValue()==1){
				ShopInfo record=new ShopInfo();
				record.setId(shopId);
				record.setStatus((byte)2);							
				shopInfoMapper.updateByPrimaryKeySelective(record);
			}
			if(opType.intValue()==2){
				//冻结
				ShopInfo record=new ShopInfo();
				record.setId(shopId);
				record.setStatus((byte)1);							
				shopInfoMapper.updateByPrimaryKeySelective(record);
			}	
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_SUCCESS);
			result.put(Constants.RETURN_MESSAGE_KEY, "success");		
			
			
		}catch(Exception ex){
			ex.printStackTrace();
			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
			result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());	
		}
		return result;
	}
	public Result del(User user, Integer shopId){
		Result result= ResultFactory.getResultFail();
		try{
			this.log.info("用户" + user.getRealName() + "  del user:" + shopId );
			if(shopId==null){
//				result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//				result.put(Constants.RETURN_MESSAGE_KEY, "数据异常");		
				result=ResultFactory.getResultFail("数据异常");
				return result;
			}
			ShopInfo model= shopInfoMapper.selectByPrimaryKey(shopId);
			if(null==model){	
				result=ResultFactory.getResultFail("未查询到门店");
				return result;
			}
			
			ShopInfo record=new ShopInfo();
			record.setId(model.getId());
			record.setDelFlag((byte)1);					
			shopInfoMapper.updateByPrimaryKeySelective(record);
			
			result=	ResultFactory.getResultSuccess();
			
			
		}catch(Exception ex){
			ex.printStackTrace();
			result=ResultFactory.getResultFail(ex.getMessage());
//			result.put(Constants.RETURN_CODE_KEY, Constants.RETURN_CODE_FAIL);
//			result.put(Constants.RETURN_MESSAGE_KEY, ex.getMessage());	
		}
		return result;
	}
}
